package com.study.service;

import com.rabbitmq.client.Channel;
import com.study.bean.MyMsg;
import com.study.bean.Order;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @author study
 * @version 1.0
 * @date 2020/8/22 14:00
 */
@Component
@RabbitListener(queues = {"hello-java-queue"})
public class ReceiveService {
    @RabbitHandler
    public void receiveHandler(Message message, Order order, Channel channel) {
        System.out.println("接收到的消息" + order);

        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        try {
            //非批量签收
            if (deliveryTag % 2 == 0) {
                channel.basicAck(deliveryTag, false);
            } else {
                //拒收获取| 批量，重新入队（requeue=true 发回服务器，重新入队）
                channel.basicNack(deliveryTag, false, true);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @RabbitHandler
    public void receiveHandlerMsg(MyMsg msg) {

        System.out.println("接收到的消息" + msg);
    }

}
